package org.bluedream.core.module.controller.warehouse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.entity.warehouse.WareHouse;
import org.bluedream.core.module.service.warehouse.WareHouseService;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WareHouseController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-08-24 16:06
 * @Version
 */
@Controller
@RequestMapping(value = "${adminPath}/wareHouse")
public class WareHouseController extends BaseController {
    @Resource
    private WareHouseService wareHouseService;

    @ModelAttribute
    public WareHouse get(String id){
        return wareHouseService.getById(id);
    }

    @RequiresPermissions("wareHouse:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(WareHouse entity, Model model){
        model.addAttribute("isSystem" , UserUtil.isSystem());
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("wareHouse" , entity);
        return "module/warehouse/wareHouseList";
    }

    @RequiresPermissions("wareHouse:view")
    @RequestMapping("/form")
    public String form(WareHouse entity, Model model){
        model.addAttribute("wareHouse" , entity);
        return "module/warehouse/wareHouseForm";
    }

    @RequiresPermissions("wareHouse:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<WareHouse> findPage(@RsaSecurityParameter WareHouse entity){
        return wareHouseService.findPage(entity);
    }

    @RequiresPermissions("wareHouse:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(WareHouse entity){
        boolean flag = wareHouseService.save(entity);
        if (flag){
            return "仓库信息保存成功";
        }else {
            return "仓库信息保存失败";
        }
    }

    @RequiresPermissions("wareHouse:view")
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map<String , Object>> treeData(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return wareHouseService.treeData(excludeCode , isShowCode);
    }
}
